<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\Atlas;
use App\Models\AtlasCategory;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class AtlasController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {

        return Grid::make(new Atlas(), function (Grid $grid) {
            $url = env("QINIU_IMAGE");
            $grid->model()->orderBy('id', 'desc');
            $grid->column('id')->sortable();
            $grid->column('category_name','所属类别')->label('#586cb1');
            $grid->column('title','图集名称')->width(200);
            $grid->column('image','主图')->image($url, 200, 150);
            // 显示多图
            $grid->column('atlas','多图')->display(function ($atlas) {
                return $atlas;
            })->image($url, 200, 200)->width(600);

            $grid->column('pv','访问量')->sortable();
            $grid->column('created_at','添加时间');


            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');

            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Atlas(), function (Show $show) {
            $show->field('id');
            $show->field('title','图集名称');
            $show->image('image','主图');
            $show->images('atlas','图集');
            $show->field('update_at','更新时间');
            $show->field('created_at','添加时间');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {

        //查询所有类别
        $categorys = AtlasCategory::all('id','name as text')->toArray();
        $categorys = array_column($categorys,'text','id');

        return Form::make(new Atlas(), function (Form $form) use ($categorys) {

            $form->submitted(function (Form $form) {

                $category_id = $form->category_id;
                $category = AtlasCategory::where('id',$category_id)->first();
                if($category){
                    $form->category_id = $category_id;
                    $form->category_name = $category->name;
                }

            });

            $form->display('id');
            $form->hidden('category_name');
            $form->select('category_id','所属类别')->options($categorys);
            $form->text('title','图集名称');
            $form->image('image','主图')->uniqueName()->autoUpload();
            $form->multipleImage('atlas','图集')->uniqueName()->autoUpload();//multipleImage-多图
            $form->display('created_at','添加时间');
            $form->display('updated_at','更新时间');




            $form->disableEditingCheck();
            $form->disableCreatingCheck();
            $form->disableViewCheck();
            $form->disableResetButton();

        });


    }
}
